﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarLoanSystem
{
    public partial class panelInterrest : UserControl
    {
        public panelInterrest()
        {
            InitializeComponent();
            this.gvEmpInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            gvEmpInfo.AutoGenerateColumns = false;

            //Set Columns Count
            gvEmpInfo.ColumnCount = 4;

            gvEmpInfo.Columns[0].Width = 40;

            //Add Columns
            gvEmpInfo.Columns[1].Name = "INTERREST_ID";
            gvEmpInfo.Columns[1].HeaderText = "รหัส";
            gvEmpInfo.Columns[1].DataPropertyName = "INTERREST_ID";
            gvEmpInfo.Columns[1].Width = 35;

            gvEmpInfo.Columns[2].Name = "INTERREST_RATE";
            gvEmpInfo.Columns[2].HeaderText = "อัตราดอกเบี้ย";
            gvEmpInfo.Columns[2].DataPropertyName = "INTERREST_RATE";
            gvEmpInfo.Columns[2].Width = 100;

            gvEmpInfo.Columns[3].Name = "DEBT_MONTH";
            gvEmpInfo.Columns[3].HeaderText = "จำนวนเดือน";
            gvEmpInfo.Columns[3].DataPropertyName = "DEBT_MONTH";
            gvEmpInfo.Columns[3].Width = 100;

            gvEmpInfo.DataSource = LoadDownPay();
        }

        private DataTable LoadDownPay()
        {
            DataTable dt = DAL.InterrestInfoDAL.LoadAllInterrest(this.txtDownId.Text,this.txtDownRate.Text);
            return dt;
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                if (((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value != null && !((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value.ToString().Equals(string.Empty))
                {
                    frmEditInterrest frmEditInterrest = new frmEditInterrest(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString());
                    frmEditInterrest.ShowDialog();
                    gvEmpInfo.DataSource = LoadDownPay();
                }
                else
                {
                    gvEmpInfo.DataSource = LoadDownPay();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddInterrest frmAddInterrest = new frmAddInterrest();
            frmAddInterrest.ShowDialog();
            gvEmpInfo.DataSource = LoadDownPay();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvEmpInfo.DataSource = LoadDownPay();
        }
    }
}
